#-*- coding:utf-8 -*-

import commands

def write_data(filename, data):
    fo = open(filename, 'a')
    fo.write('\n' + data)
    fo.close()

def collect_Data(cmd,fileNum, fileURL, count, fileType = 'normal'):
    '''
    :param cmd: 采集数据的命令
    :param fileType: 保存文件的类型，异常数据（被攻击的数据），正常数据
    :param fileNum: 采集数据保存文件的数量
    :param fileURL: 保存文件的路径
    :param count: 采集数据的数量
    :return:
    '''
    #cmd = 'ping -c 10 www.baidu.com'
    for i in range(0,fileNum):
        # 生成文件名称,fileType为normal，则生成正常文件，fileType为abnormal 保存异常文件
        if fileType == 'normal':
            filename = fileURL+'normalData/normal'+str(i)
        elif fileType == 'abnormal':
            filename = fileURL+'abnormalData/abnormal'+str(i)
        for count1 in range(0,count):
            return_code, output = commands.getstatusoutput(cmd)
            print count1
            ol = output.split('\n')
            ln_data = ['0', '0', '0', '0', '0', '0', '0', '0', '0', '0']
            i = 0
            for info in ol:
                if 'time' in info and 'bytes' in info:
                    t = info.split(' ')[7]
                    #print len(t)
                    nt = t[:len(t) - 2]
                    # print nt
                    # if float(nt) > 1000:
                    #     continue
                    ln_data[i] = str(nt)
                    i = i + 1
            wdata = ''
            for nd in ln_data:
                if wdata == '':
                    wdata = nd
                else:
                    wdata = wdata + ' ' + nd
            write_data(filename, wdata)
        print 'this is the FIlE:',filename
        #f = open(filename,"w")
        #f.write("something")
       # f.close()
#collect_Data(cmd='ping -c 10 www.baidu.com', fileNum=20, fileURL='/home/hh/desk/collectData/',count=10)
collect_Data(cmd='sudo l2ping -c 10 48:2C:A0:0F:1F:AD', fileNum=20, fileURL='/home/hh/desk/collectData/',count=600)
